<?php

namespace xmi_shop\admin\model;
use xmi_shop\model;
class orderModel extends model
{

    public static function get_list( $filter = [] ){

        $sql = 'select o.*, os.name as status_text from sh_order o left join sh_order_status os on o.status=os.code order by o.id desc ';

//        $query = self::$db->query( $sql );
//
//        return $query->rows;

        return pdo_fetchall( $sql );
    }

    public static function get_order( $order_id ){

        $sql = 'select * from sh_order where id=' . $order_id;

        $order = pdo_fetch( $sql );
        $order['goods'] = self::get_order_goods( $order_id );
        $order['total'] = self::get_order_total( $order_id );
//        $order['package'] = self::get_order_package( $order_id );
        $order['history_list'] = self::history_list( $order_id );

        if( $order['shipping_method'] == 'pickup' ){

            $order['pickup'] = self::get_order_pickup( $order_id );

        }else if( $order['shipping_method'] == 'package' ){
            $order['package'] = self::get_order_package( $order_id );
        }

        return $order;

    }

    public static function get_order_goods( $order_id ){

        $sql = 'select * from sh_order_goods where order_id=' . $order_id;
//        $query = self::$db->query( $sql );
//
//        return $query->rows;
        return pdo_fetchall( $sql );
    }

    public static function get_order_total( $order_id ){

        $sql = 'select * from sh_order_total where order_id=' . $order_id;
//        $query = self::$db->query( $sql );
//
//        return $query->rows;
        return pdo_fetchall( $sql );
    }

    public static function get_all_status(){

        $sql = "SELECT * FROM sh_order_status WHERE is_active = 1;";
//        $query = self::$db->query( $sql );
//
//        return $query->rows;
        return pdo_fetchall( $sql );
    }

    public static function add_history( $order_id, $data ){

        $sql = "INSERT INTO sh_order_history SET order_id = '" . $order_id . "',
                `comment` = '" . $data['comment'] . "',
                order_status = '" . $data['order_status'] . "',
                create_time = '" . $data['create_time'] . "'";
//        $query = self::$db->query( $sql );
        $query =  pdo_query( $sql );

        //todo 修改订单状态
        $change_sql = "UPDATE sh_order SET status= '" . $data['order_status'] . "' WHERE id= " . $order_id;

        return pdo_query( $change_sql );
    }

    public static function history_list( $order_id ){

        $sql = 'SELECT 
              h.*,
              s.`name`
            FROM
              sh_order_history h 
              LEFT JOIN sh_order_status s 
                ON h.`order_status` = s.`code` where h.order_id=' . $order_id . ' order by h.create_time desc';

//        $query = self::$db->query( $sql );
//
//        return $query->rows;
        return pdo_fetchall( $sql );
    }


    public static function get_order_pickup( $order_id ){

        $sql = "select * from sh_order_pickup where order_id=" . $order_id ;

        return pdo_fetchall( $sql );

    }

    public static function get_order_package( $order_id ){

        $sql = "select op.*,e.name from sh_order_package op left join sh_express e on op.express_code=e.code where op.order_id=" . $order_id ;

        return pdo_fetchall( $sql );
    }

    public static function addPackage( $order_id, $data ){

        $sql = "insert into sh_order_package set order_id=" . $order_id
            . ", package_no='" . $data['package_no'] . "'"
            . ", express_code='" . $data['express_code'] . "'"
        ;

        return pdo_query( $sql );
    }

    public static function removePackage( $order_id ){

        $sql = 'delete from sh_order_package where order_id=' . $order_id;

        return pdo_query( $sql );

    }
}